using System.Linq;

using vurfu.DataContracts;
using vurfu.Linq;
using vurfu.Repositories.Interfaces;

namespace vurfu.Repositories
{
    public class GroupRepository : IGroupRepository
    {
        public Group GetGroup(string groupId)
        {
            Group group;
            using (var connection = DataContextFactory.Create())
            {
                group = (from groupRecord in connection.GetTable<Group>()
                             where groupRecord.GroupId == groupId
                             select groupRecord).First();
            }
            return group;
        }

        public StaticGroup GetStaticGroup(string groupId)
        {
            StaticGroup group;
            using (var connection = DataContextFactory.Create())
            {
                group = (from groupRecord in connection.GetTable<StaticGroup>()
                             where groupRecord.GroupId == groupId
                             select groupRecord).First();
            }
            return group;
        }

        public void SaveStaticGroup(StaticGroup group)
        {
            if (!group.Active.HasValue)
                group.Active = true;
            using(var connection = DataContextFactory.Create())
            {
                connection.GetTable<StaticGroup>().InsertOnSubmit(group);
                connection.SubmitChanges();
            }
        }

        public void SaveGroup(Group group)
        {
            if (!group.Active.HasValue)
                group.Active = true;
            using (var connection = DataContextFactory.Create())
            {
                connection.GetTable<Group>().InsertOnSubmit(group);
                connection.SubmitChanges();
            }
        }

        public void ChangeGroup(Group group)
        {
            if (!group.Active.HasValue)
                group.Active = true;
            var groupId = group.GroupId;
            using (var connection = DataContextFactory.Create())
            {
                var oldGroup = (from groupRecord in connection.GetTable<Group>()
                                where groupRecord.GroupId == groupId
                                select groupRecord).First();
                oldGroup.Active = group.Active;
                oldGroup.GroupDescription = group.GroupDescription;
                connection.SubmitChanges();
            }
        }

        public void ChangeStaticGroup(StaticGroup group)
        {
            if (!group.Active.HasValue)
                group.Active = true;
            var groupId = group.GroupId;
            using (var connection = DataContextFactory.Create())
            {
                var oldGroup = (from groupRecord in connection.GetTable<StaticGroup>()
                         where groupRecord.GroupId == groupId
                         select groupRecord).First();
                oldGroup.Active = group.Active;
                oldGroup.GroupDescription = group.GroupDescription;
                connection.SubmitChanges();
            }
        }
    }
}